/*
 * Copyright 2017 Matthew Tamlin
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.matthewtamlin.fortytwo.library_tests;

import ohos.aafwk.ability.Ability;
import ohos.aafwk.content.Intent;
import ohos.aafwk.content.Operation;
import ohos.agp.colors.RgbColor;
import ohos.agp.components.AttrHelper;
import ohos.agp.components.Button;
import ohos.agp.components.Component;
import ohos.agp.components.ComponentContainer;
import ohos.agp.components.DirectionalLayout;
import ohos.agp.components.Text;
import ohos.agp.components.element.ShapeElement;
import ohos.agp.text.Font;
import ohos.agp.utils.Color;
import ohos.agp.window.service.WindowManager;

/**
 * Entry point for manual testing. This class launches the test harnesses.
 */
public class LauncherActivity extends Ability {
    @Override
    protected void onStart(Intent intent) {
        super.onStart(intent);

        getWindow().setStatusBarVisibility(Component.HIDE);
        WindowManager.getInstance().getTopWindow().get().setStatusBarColor(Color.getIntColor("#666666"));

        final DirectionalLayout rootView = new DirectionalLayout(this);
        rootView.setOrientation(DirectionalLayout.VERTICAL);
        rootView.setLayoutConfig(new DirectionalLayout.LayoutConfig(ComponentContainer.LayoutConfig.MATCH_PARENT,
            ComponentContainer.LayoutConfig.MATCH_PARENT));
        setUIContent(rootView);

        rootView.addComponent(createTabTitle());
        rootView.addComponent(createLine());
        rootView.addComponent(createLaunchTestSimpleAnswerCard());
        rootView.addComponent(createLaunchMultipleChoiceAnswerGroupTestHarnessButton());
    }

    private Text createTabTitle() {
        Text tv = new Text(this);
        tv.setText(ResourceTable.String_library_tests_library);
        tv.setLayoutConfig(new DirectionalLayout.LayoutConfig(ComponentContainer.LayoutConfig.MATCH_PARENT,
            AttrHelper.vp2px(60, this)));
        tv.setFont(Font.DEFAULT_BOLD);
        int padding = AttrHelper.vp2px(10, this);
        tv.setPadding(padding, padding, padding, padding);
        tv.setTextSize(AttrHelper.fp2px(18, this));
        tv.setTextColor(new Color(Color.getIntColor("#333333")));
        return tv;
    }

    private Component createLine() {
        Component component = new Component(this);
        component.setLayoutConfig(new DirectionalLayout.LayoutConfig(ComponentContainer.LayoutConfig.MATCH_PARENT,
            AttrHelper.vp2px(1, this)));
        ShapeElement shapeElement = new ShapeElement();
        shapeElement.setRgbColor(RgbColor.fromArgbInt(Color.getIntColor("#e5e5e5")));
        component.setBackground(shapeElement);
        component.setMarginBottom(5);
        return component;
    }

    /**
     * Creates a button which launches the {@link SimpleAnswerCardTestHarness} activity when
     * pressed.
     *
     * @return the button, not null
     */
    private Button createLaunchTestSimpleAnswerCard() {
        final Button b = new Button(this);
        configBtn(b);
        b.setText("Launch DecoratedAnswerCard test harness");

        b.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                startActivity(DecoratedAnswerCardTestHarness.class);
            }
        });

        return b;
    }

    /**
     * Creates a button which launches the {@link SelectionLimitAnswerGroupTestHarness} activity
     * when pressed.
     *
     * @return the button, not null
     */
    private Button createLaunchMultipleChoiceAnswerGroupTestHarnessButton() {
        final Button b = new Button(this);
        configBtn(b);
        b.setText("Launch MultipleChoiceAnswerGroup test harness");

        b.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                startActivity(SelectionLimitAnswerGroupTestHarness.class);
            }
        });

        return b;
    }

    private void configBtn(Button b) {
        final int padding = 20;
        final int paddingTop = 25;
        final int textSize = 40;
        b.setPadding(padding, paddingTop, padding, paddingTop);
        b.setTextSize(textSize);
        b.setTextColor(new Color(Color.getIntColor("#333333")));
        b.setBackground(new ShapeElement(getContext(), ResourceTable.Graphic_btn_bg_normal));
        b.setLayoutConfig(new ComponentContainer.LayoutConfig(ComponentContainer.LayoutConfig.MATCH_PARENT,
            ComponentContainer.LayoutConfig.MATCH_CONTENT));

        final int margin = 10;
        b.setMarginRight(margin);
        b.setMarginBottom(margin);
        b.setMarginLeft(margin);
        b.setMarginTop(margin);

        b.setFont(Font.DEFAULT_BOLD);
    }

    private void startActivity(Class<?> cls) {
        Intent intent = new Intent();

        // 通过Intent中的OperationBuilder类构造operation对象，指定设备标识（空串表示当前设备）、应用包名、Ability名称
        Operation operation = new Intent.OperationBuilder()
            .withBundleName(getBundleName())
            .withAbilityName(cls)
            .build();

        // 把operation设置到intent中
        intent.setOperation(operation);
        startAbility(intent);
    }
}